import {lock,unlock,getLock} from '../../utils/lock'
import {description, path, request, summary, tagsAll} from "koa-swagger-decorator-fork"

@tagsAll(['LockFile'])
export default class LockFileRouter {
    @request('GET', '/files/lock/{id}')
    @summary('get lock user by file id')
    @path({ id: { type: 'string', required: true } })
    @description('查询文件锁定')
    static async getLockUser(ctx) {
        let userid = await getLock(ctx.params.id)
        ctx.body = userid?{userid}:{}
    }

    @request('POST', '/files/lock/{id}')
    @summary('lock file by file id')
    @path({ id: { type: 'string', required: true } })
    @description('锁定文件')
    static async lockFile(ctx) {
        await lock(ctx.params.id,ctx.user.id)
        ctx.body = {}
    }

    @request('DELETE', '/files/lock/{id}')
    @summary('unlock file by file id')
    @path({ id: { type: 'string', required: true } })
    @description('解锁文件')
    static async unlockFile(ctx) {
        await unlock(ctx.params.id,ctx.user.id)
        ctx.body = {}
    }
}
